﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Jinjia.Approve.IService.JJFlow;
using Jinjia.Approve.Model.Input.JJFlow;
using Jinjia.Approve.Model.VM.JJFlow;
using JinJia.Core.Base.Exceptions;
using Microsoft.AspNetCore.Http;

namespace Jinjia.Approve.Service.JJFlow
{
    public class JJFlowCommonService
    {
        protected readonly IHttpContextAccessor _httpContextAccessor;
        protected readonly IJJFlowClientService _jjFlowClientService;

        public JJFlowCommonService(IHttpContextAccessor httpContextAccessor, IJJFlowClientService jjFlowClientService)
        {
            _httpContextAccessor = httpContextAccessor;
            _jjFlowClientService = jjFlowClientService;
        }

        protected virtual void BizOrderIdChecker(string biz_order_id)
        {
            if (string.IsNullOrWhiteSpace(biz_order_id))
            {
                throw new BusinessException("业务ID不能为空！");
            }
        }

        /// <summary>
        /// 模块配置检查
        /// </summary>
        /// <param name="modular_key"></param>
        /// <returns></returns>
        /// <exception cref="BusinessException"></exception>
        protected virtual async Task<int> TemplateByModularKeyChecker(string modular_key)
        {
            var template_id = await _jjFlowClientService.GetTemplateID(modular_key);
            if (template_id == 0)
            {
                throw new BusinessException("未找到该模块配置的审批流信息！");
            }

            return template_id;
        }

        /// <summary>
        /// 操作人id校验
        /// </summary>
        /// <param name="t"></param>
        /// <typeparam name="T"></typeparam>
        /// <exception cref="BusinessException"></exception>
        protected virtual void RequestUserIdChecker<T>(T t) where T : JJFlowRequestCommon
        {
            if (t.user_id == 0)
            {
                var userId = GetUserId();
                if (userId > 0 || userId == -1)
                {
                    t.user_id = userId;
                }
                else
                {
                    throw new BusinessException("未传入操作人！");
                }
            }
        }

        /// <summary>
        /// 申请记录查询校验
        /// </summary>
        /// <param name="modular_key"></param>
        /// <param name="biz_order_id"></param>
        /// <returns></returns>
        /// <exception cref="BusinessException"></exception>
        protected virtual async Task<JjFlowInstanceVM> InstanceInfoChecker(string modular_key, string biz_order_id)
        {
            //获取申请记录ID
            var instance_info = await _jjFlowClientService.GetInstanceInfo(modular_key, biz_order_id);
            if (instance_info == null || instance_info.Id == 0)
            {
                throw new BusinessException("未找到申请记录");
            }

            return instance_info;
        }


        protected int GetUserId()
        {
            var id = GetClaim("id");
            var i = int.Parse(id);
            return i;
        }

        private string GetClaim(string type)
        {
            return _httpContextAccessor?.HttpContext?.User?.Claims?.FirstOrDefault(x => x.Type == type)?.Value;
        }
    }
}